package com.ckzp.jfinal.system.controller;

import com.ckzp.core.util.DateUtil;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.system.cache.SysUserCache;
import com.ckzp.jfinal.system.model.SysCode;
import com.ckzp.jfinal.system.util.SysOnlineUtil;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.plugins.UpImage;
import com.ckzp.jfinal.system.model.SysOnline;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2020/6/1.
 */
public class SysOnlineController extends BaseController {

    @Admins(roles = "system", name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 在线用户列表
     */
    @Admins(roles = "system", name = "在线用户列表PAGE", log = false)
    public void SysOnlineList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysOnlineList.html");
    }

    /**
     * 在线用户列表查询API
     */
    @Admins(roles = "system", name = "在线用户列表查询API", log = false)
    public void SysOnlineListDataApi() {
        PageSearch ps = PageSearch.n().setTable("sys_online").init(getRequest())
                .orderby(getRequest(), "last_active_int desc");
        Page<SysOnline> pages = ps.paginate(SysOnline.dao);
        List<SysOnline> datalist = pages.getList();

        for (SysOnline online : datalist) {
            Date last_login = online.getDate("last_login");
            Date last_active = online.getDate("last_active");
            online.put("times", DateUtil.getDateBetween(last_active, last_login));
        }
        SysOnlineUtil util = SysOnlineUtil.n();
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()).setMsg(util.getOnlineUserInfo()));
    }


    /**
     * 字段更新通用接口
     */
    @Admins(roles = "system", name = "修改fieldApi", log = true)
    public void ChangeTag() {
        String data_key = getParaStr("data_key", "");
        String colums = getParaStr("colums", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysOnline.dao, data_key, colums, msg));
    }
}